package com.lanou.remember.service.impl;

import com.lanou.k12.entity.Record;
import com.lanou.k12.entity.ResultObject;
import com.lanou.k12.entity.User;
import com.lanou.remember.mapper.RecordMapper;
import com.lanou.remember.service.IRecordService;
import com.lanou.remember.utils.GetLoginUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class RecordService implements IRecordService {

    @Autowired
    RecordMapper mapper;

    @Override
    //返回用户需复习题数量
    public ResultObject remind() {
//        User u = (User) session.getAttribute("user");
        User u = (User) SecurityUtils.getSubject().getPrincipal();
        ResultObject obj = new ResultObject();
        int uid = u.getId();
        if (mapper.findUser(uid)!=0) {
            int count = mapper.remind(uid);
            if(count > 0) {
                obj.setStatus(1);
                obj.setData(count);
            }
            if(count <= 0) {
                obj.setStatus(0);
                obj.setInfo("暂无复习内容");
            }
            return obj;
        }
        obj.setStatus(0);
        obj.setInfo("先去学一会新知识吧");
        return obj;
    }

    @Override
    public ResultObject findRecord() {
        List<Record> list = mapper.findRecord(GetLoginUser.getLoginUser().getId());
        ResultObject obj = new ResultObject();
        if (list == null) {
            obj.setStatus(0);
            obj.setInfo("没有复习计划");
        } else {
            obj.setStatus(1);
            HashMap<String,Integer> map = new HashMap<>();
            int a;
            int b;
            int c;
            int d;
            int e;
            a=b=c=d=e=0;
            for (Record r : list) {
                if(r.getReview() >= 0) {
                    map.put("已学习",++a);
                }
                if(r.getReview() == 1) {
                    map.put("了解",++b);
                }
                if(r.getReview() == 2 ) {
                    map.put("熟悉",++c);
                }
                if(r.getReview() == 3 ) {
                    map.put("理解",++d);
                }
                if(r.getReview() == 4 ) {
                    map.put("掌握",++e);
                }
            }
            obj.setData(map);
        }
        return obj;

    }

    @Override
    public ResultObject addRecord(int qid) {
        Record r = new Record();
        r.setUid(GetLoginUser.getLoginUser().getId());
        r.setQid(qid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        r.setDate(date);
        ResultObject obj = new ResultObject();
        obj.setStatus(mapper.addRecord(r));
        return obj;

    }

    @Override
    public ResultObject updateRecord(int qid) {
        Record r = new Record();
        r.setUid(GetLoginUser.getLoginUser().getId());
        r.setQid(qid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        r.setDate(date);
        ResultObject obj = new ResultObject();
        obj.setStatus(mapper.updateRecord(r));
        return obj;
    }


}
